﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using SevenShine;
using System;
using UnityEngine.SceneManagement;

public class Level6BeginEnd : MonoBehaviour {

    public static Level6BeginEnd Instance;
    public Animator ani;
    public Text text;
    public GameObject XiaoQi;
    public Transform camera;
    public Transform Pos2;
    public Transform Pos3;
    public Transform Pos4;
    public GameObject btnObj;
    public GameObject ball;
    //public AudioClip TypeWritingClip;
    public AudioClip RunClip;
    public AudioClip PassClip;
    public AudioClip OpenDoorClip;
    public AudioClip DialogClip;

    private AudioSource audios;

	// Use this for initialization
	void Start ()
    {
        Instance = this;
        audios = Camera.main.GetComponent<AudioSource>();
        Begin();
	}

    void Begin()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(Introduce(() =>
        {
            StartCoroutine(Dialog(()=>
            { 
                StartCoroutine(Run(() =>
                {
                    StartCoroutine(BeginGame(() =>
                    {
                        Debug.Log("Animation Finish!");
                    }));
            }));
        }));
    }));
        
    }

    IEnumerator Dialog(Action action = null)
    {
        yield return new WaitForSeconds(0.5f);
        Instantiate(Resources.Load("Prefabs/UI/DialogImage"), transform);
        GameObject DialogPanel = GameObject.Find("Canvas/DialogImage(Clone)");
        Text dialogText = GameObject.Find("Canvas/DialogImage(Clone)/Text").GetComponent<Text>();

        audios.clip = DialogClip;
        audios.Play();
        audios.loop = false;
        float length = DialogClip.length;

        dialogText.DOText(LanguageMgr.GetInstance().ShowText("Level6_Dialog"), length).SetEase(Ease.Linear).OnComplete(() =>
        {
            DialogPanel.SetActive(false);
            if (action != null)
            {
                action();
            }
        });
    }

    public void EndGame()
    {
        camera.parent = Pos4;
        camera.localPosition = new Vector3(0,0,0);
        camera.localEulerAngles = new Vector3(0,0,0);
        StartCoroutine(OpenDoor(() =>
        {
            StartCoroutine(EndDialog(() =>
            { 
            }));
        }));
    }

    IEnumerator OpenDoor(Action action = null)
    {
        yield return new WaitForSeconds(0);

        //开门特效
        GameObject effect = Instantiate(Resources.Load("Prefabs/Effect/Level006_GateSmoke"),GameObject.Find("Road").transform) as GameObject;
        effect.transform.localPosition = new Vector3(0,0,0);

        //开门音效
        audios.clip = OpenDoorClip;
        audios.Play();
        audios.loop = false;

        GameObject.Find("Door_Level006_R").transform.DOLocalMoveX(0.65f,3).SetEase(Ease.Linear);
        GameObject.Find("Door_Level006_L").transform.DOLocalMoveX(-0.65f, 3).SetEase(Ease.Linear).OnComplete(()=> 
        {
            audios.Stop();

            //分数
            GameObject.Find("UI_Game").SendMessage("AddNum", 6);
            //关卡解锁
            if (!LevelSingleton.GetInstance().levelData.ContainsKey(6))
            {
                LevelSingleton.GetInstance().levelData.Add(6, 1);
            }
            
            //过关音效
            audios.clip = PassClip;
            audios.Play();
            audios.loop = false;

            PlayerPrefs.SetInt("6", 1);
            if (action != null)
            {
                action();
            }
        });

    }

    IEnumerator EndDialog(Action action = null)
    {
        yield return new WaitForSeconds(0);
        GameObject obj= Instantiate(Resources.Load("Prefabs/UI/DialogImage"),gameObject.transform) as GameObject;
        Text text = obj.GetComponentInChildren<Text>();
        text.text = "";



        text.DOText(LanguageMgr.GetInstance().ShowText("Level6_Dialog"),2).SetEase(Ease.Linear).OnComplete(()=> 
        {
            SceneManager.LoadScene("007");
            if (action != null)
            {
                action();
            }
        });
    }

    IEnumerator Introduce(Action action=null)
    {
        yield return new WaitForSeconds(0);

        //audios.clip = TypeWritingClip;
        //audios.Play();
        //audios.loop = true;

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level6_Panbai"), 2).SetEase(Ease.Linear).OnComplete(() =>
        //{
        //    audios.Stop();
        //    text.gameObject.SetActive(false);
        //    if (action != null)
        //    {
        //        action();
        //    }
        //});

        text.text = LanguageMgr.GetInstance().ShowText("Level6_Panbai");
        yield return new WaitForSeconds(2);
        text.gameObject.SetActive(false);
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Run(Action action = null)
    {
        yield return new WaitForSeconds(1);
        ani.SetTrigger("Run");

        audios.clip = RunClip;
        audios.Play();

        StartCoroutine("ChangeCamera");

        XiaoQi.transform.DOMoveZ(4,1).SetEase(Ease.Linear).OnComplete(()=> 
        {
            ani.SetTrigger("Idle");
            audios.Stop();
            btnObj.transform.DOLocalMoveY(-0.035f,0.2f);
            GameObject.Find("Cellars").GetComponent<Animator>().enabled = true;

            if (action != null)
            {
                action();
            }
        });
    }

    IEnumerator ChangeCamera()
    {
        yield return new WaitForSeconds(0.4f);
        camera.parent = Pos2;
        camera.localPosition = new Vector3(0, 0, 0);
        camera.localEulerAngles = new Vector3(0, 0, 0);
    }

    IEnumerator BeginGame(Action action = null)
    {
        yield return new WaitForSeconds(2f);
        camera.parent = Pos3;
        camera.transform.localPosition = new Vector3(0,0,0);
        camera.transform.localEulerAngles = new Vector3(0,0,0);
        
        //GameObject.Find("Cellars").GetComponent<Animator>().enabled=true;
        ball.SetActive(true);
        if (action != null)
        {
            action();
        }
    }
}
